package merchantCard

import (
	"encoding/json"
	"fmt"
	wxcard "github.com/chanxuehong/wechat/mp/card"
	"github.com/mjiulee/lego/utils"
	cardconfjson "saas_sys_base/modules/merchantCard/models/cardconf/cardconfjson"
)

const (
	CARD_TEMPLATE_ID_MEMBER_CARD_100 = 100
)

var (
	FALSE_VAR bool = false
	TRUE_VAR bool = true
)

type CardConf_1000 struct {
	WxCardMsg *wxcard.CardMsg

}

var CardConf_1000_Val CardConf_1000


func GetCardParaByTempate_1000(card *MchCardUnionMsg) (newCardPard *MchCardUnionMsg, err error) {
	newCard := NewMchCardUnionMsg(wxcard.CardTypeMemberCard)
	if card == nil {

	}

	*newCard = *card

	newCard.Card = CardConf_1000_Val.WxCardMsg.Card
	newCard.CardMode = CARD_MODE_MCH //CARD_MODE_MCH_WECHAT

	retCardPara :=new(MchCardUnionMsg)
	//注意：这里一定要深拷贝，因为后面引用这个返回值时，可能回修改返回值的内容，结构里面有一些指针，所以会修改一些共享引用的值
	err = utils.DeepCopy(retCardPara, newCard)
	if err != nil {
		panic(err)
	}

	return retCardPara, nil
}

//继承自card 1000，区别是1001为商户卡兼容微信卡模式
func GetCardParaByTempate_1001(card *MchCardUnionMsg) (newCardPard *MchCardUnionMsg, err error) {
	newCard, err := GetCardParaByTempate_1000(card) //继承card para 1000
	if err != nil {
		return nil, err
	}

	newCard.CardMode = CARD_MODE_MCH_WECHAT
	return newCard, nil
}


//继承自card 1000，区别是1001为微信卡模式
func GetCardParaByTempate_1002(card *MchCardUnionMsg) (newCardPard *MchCardUnionMsg, err error) {
	newCard, err := GetCardParaByTempate_1000(card) //继承card para 1000
	if err != nil {
		return nil, err
	}

	newCard.CardMode = CARD_MODE_WECHAT
	return newCard, nil
}

/*
根据卡的参数模板，配置卡参数
 */
func GetCardParaByCardPara(card *MchCardUnionMsg) (newCardPard *MchCardUnionMsg, err error) {

	if card.CardParaTemplateId == CARD_TEMPLATE_ID_MEMBER_CARD_100 {
		//参数校验，必须是配置
		if card.Card.CardType != wxcard.CardTypeMemberCard {
			return nil, fmt.Errorf("CardType must be MemberCard when card para template is <%d>", card.CardParaTemplateId)
		}

		newCardPard, err = GetCardParaByTempate_1000(card)
		return
	}

	return nil, fmt.Errorf("No support card para template id <%d>", card.CardParaTemplateId)
}

func LoadCardConf() {
	newCardConf := new(wxcard.CardMsg)
	err := json.Unmarshal([]byte(cardconfjson.CardConfJSON_1000_WX_MEMBERCARD), newCardConf)
	if err != nil {
		panic(fmt.Sprintf("LoadCardConf<%s>", err.Error()))
	}
	CardConf_1000_Val.WxCardMsg = newCardConf

}

func init() {
	LoadCardConf()
}

/*

newCard.Card.CardType = wxcard.CardTypeMemberCard
	newCard.CardMode = CARD_MODE_MCH //CARD_MODE_MCH_WECHAT
	newCard.Card.MemberCard = new(wxcard.MemberCard)
	backgoundPicUrl := "https://mmbiz.qlogo.cn/mmbiz/"
	newCard.Card.MemberCard.BackgoundPicUrl = &backgoundPicUrl
	newBaseInfo := new(wxcard.CardBaseInfo)

	newBaseInfo.LogoURL = "http://mmbiz.qpic.cn/mmbiz/iaL1LJM1mF9aRKPZ/0"
	newBaseInfo.BrandName = "千麦客"
	newBaseInfo.CodeType =  "CODE_TYPE_TEXT"
	newBaseInfo.Title = "千麦客xxxxxxxxx"
	newBaseInfo.Color = "Color010"
	newBaseInfo.Notice = "使用时向服务员出示此券"
	newBaseInfo.ServicePhone = "020-88888888"
	newBaseInfo.Description = "不可与其他优惠同享"
	newBaseInfo.DateInfo.Type = "DATE_TYPE_PERMANENT"

	newBaseInfo.SKU.Quantity = 50000000  //<------注意，这里是库存数据
	newBaseInfo.GetLimit = 1 //每位用户只能领取一张会员卡


	newBaseInfo.UseCustomCode = &FALSE_VAR
	newBaseInfo.CanGiveFriend = &TRUE_VAR

	newBaseInfo.LocationIdList = []int64{}

	newBaseInfo.CustomURLName = "立即使用"
	newBaseInfo.CustomURL = "http://weixin.qq.com"
	newBaseInfo.CustomURLSubTitle = "6个汉字tips"

	newCard.Card.MemberCard.BonusRule = &wxcard.BonusRuleField{
		CostMoneyUnit: 100,
		IncreaseBonus: 1,
		MaxIncreaseBonus: 200,
		InitIncreaseBonus: 10,
		CostBonusUnit: 5,
		ReduceMoney: 100,
		LeastMoneyToUseBonus: 1000,
		MaxReduceBonus: 50,
	}




	var discount int = 10 //10表示九折
	newCard.Card.MemberCard.Discount = &discount
 */